package com.mb.app.appused.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;

public abstract class SplashActivity extends Activity {

	SplashLayout groupLayout;

	public abstract SplashConfig getConfig();

	public abstract Class<?> getMain();

	public abstract int getLayout();

	public abstract void getAnimation();

	SplashConfig config;

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		config = getConfig();

		if (config == null) {
			toMain();
			return;
		}

		if (config.full) {
			getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
					WindowManager.LayoutParams.FLAG_FULLSCREEN);
			requestWindowFeature(Window.FEATURE_NO_TITLE);
		}
		if (getLayout() == 0) {
			groupLayout = new SplashLayout(this);
			groupLayout.setLayoutParams(getFall());

			setContentView(groupLayout);
			ImageView img = new ImageView(this);
			groupLayout.addView(img, getFall());
			if (config.getDrawable() != -1) {
				img.setBackgroundDrawable(getResources().getDrawable(config.getDrawable()));
			}
		} else {
			setContentView(getLayout());
		}

		new Thread(new Runnable() {

			public void run() {

				try {
					Thread.sleep((long) config.time);
					toMain();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();

	}

	void toMain() {
		Intent intent = new Intent(this, getMain());
		finish();
		startActivity(intent);
		getAnimation();
	}

	LayoutParams getFall() {
		LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		return lp;
	}

	public class SplashConfig {
		private int drawable = -1;
		private float time;
		private boolean full = false;

		public SplashConfig(int drawable, float time, boolean full) {
			this.drawable = drawable;
			this.time = time;
			this.full = full;
		}

		public int getDrawable() {
			return drawable;
		}

		public void setDrawable(int drawable) {
			this.drawable = drawable;
		}

		public float getTime() {
			return time;
		}

		public void setTime(float time) {
			this.time = time;
		}

		public boolean isFull() {
			return full;
		}

		public void setFull(boolean full) {
			this.full = full;
		}

	}

	class SplashLayout extends FrameLayout {

		public SplashLayout(Context context) {
			super(context);
			// TODO Auto-generated constructor stub
		}

		public SplashLayout(Context context, AttributeSet attrs) {
			super(context, attrs);
			// TODO Auto-generated constructor stub
		}

		public SplashLayout(Context context, AttributeSet attrs, int defStyleAttr) {
			super(context, attrs, defStyleAttr);
			// TODO Auto-generated constructor stub
		}
	}
}
